package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.SysCompan;
import com.taobao.machinesystem.entity.vo.SysCompanQueryVo;
import com.taobao.machinesystem.service.SysCompanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/sys-compan")
public class SysCompanController {

    @Autowired
    private SysCompanService sysCompanService;

    /**
     * 公司表 分页查询 查看详情
     * @param currentPage
     * @param size
     * @param sysCompanQueryVo
     * @return Result
     */
    @GetMapping("/list/{currentPage}/{size}/")
    public Result list(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size") Integer size,
             SysCompanQueryVo sysCompanQueryVo) {


        Page<SysCompan> sysCompanPage = sysCompanService.listSysCompan(currentPage, size, sysCompanQueryVo);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total", sysCompanPage.getTotal())
                .setMapValue("items", sysCompanPage.getRecords());
    }

    @PostMapping("/save")
    public Result saveSysCompan(@RequestBody SysCompan sysCompan){
        if (!Objects.isNull(sysCompan)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        boolean flag = sysCompanService.save(sysCompan);

        if (flag) {
            log.info("inset sysCompan success {}", sysCompan.toString());
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        } else {
            log.error("inset sysCompan fail {}", sysCompan.toString());
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }
    /**
     * 更改信息
     * @param sysCompan
     * @return
     */
    @PutMapping("/update")
    public Result updateSysCompan(@RequestBody SysCompan sysCompan){
        if (!Objects.isNull(sysCompan) ) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = sysCompanService.updateById(sysCompan);

        if (flag) {
            log.info("update plan0 success {}", sysCompan.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", sysCompan.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除公司
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteSysCompan(@PathVariable String id){
        boolean flag = sysCompanService.removeById(id);
        if (flag) {
            log.info("update plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("update plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }
}

